package ru.kreda.webapp.client.view;

import ru.kreda.webapp.client.data.CustomListStore;
import ru.kreda.webapp.client.data.CustomPagingListLoader;
import ru.kreda.webapp.client.logic.IOrderListPresenter;
import ru.kreda.webapp.client.logic.OrderListActivity.ISettingsFormDisplay;
import ru.kreda.webapp.shared.model.OrderModel;

import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Widget;

/**
 * UI формы списка заявок.
 */
public class OrderListForm extends BaseContentPanel implements ISettingsFormDisplay {

	private static OrderListFormUiBinder uiBinder = GWT.create(OrderListFormUiBinder.class);

	/**
	 * Интерфейс генерации формы (см {@link UiBinder}).
	 */
	interface OrderListFormUiBinder extends UiBinder<Widget, OrderListForm> {
	}

	/**
	 * Переменная формы (UiBinder): корневая панель
	 */
	@UiField
	ContentPanel contentPanel;
	/**
	 * Переменная формы (UiBinder): таблица
	 */
	@UiField
	Grid<OrderModel> grid;
	@UiField
	PagingToolBar toolBar;

	private IOrderListPresenter presenter;

	/**
	 * Конструктор.
	 */
	public OrderListForm() {
		setWidget(uiBinder.createAndBindUi(this));
		grid.getView().setAutoFill(true);
		contentPanel.setLayout(new FitLayout());

		GridCellRenderer<OrderModel> clientRenderer = new GridCellRenderer<OrderModel>() {

			@Override
			public Object render(OrderModel model, String property, ColumnData config,
					int rowIndex, int colIndex, ListStore<OrderModel> store, Grid<OrderModel> grid) {
				String value = (String) model.get(property);
				final Long clientId = model.getClientId();
				Anchor anchor = new Anchor(true);
				anchor.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						Window.alert("Go to client #" + clientId);
					}
					
				});
				anchor.setText(value);
				return anchor;
			}

		};
		grid.getColumnModel().getColumn(1).setRenderer(clientRenderer);
	}

	@UiFactory
	ListStore<OrderModel> provideListStore() {
		toolBar = new PagingToolBar(50);
		return new CustomListStore<OrderModel>();
	}

	@UiFactory
	PagingToolBar createPagingToolbar() {
		return toolBar;
	}

	@Override
	public void setPresenter(IOrderListPresenter presenter) {
		this.presenter = presenter;
		contentPanel.setHeading(this.presenter.getGroupName());
	}
	
	@Override
	public void setGridLoader(CustomPagingListLoader<PagingLoadResult<OrderModel>> loader) {
		toolBar.bind(loader);
		getGridStore().bind(loader);
	}

	@Override
	public CustomListStore<OrderModel> getGridStore() {
		return (CustomListStore<OrderModel>) grid.getStore();
	}
	
}
